package controller;

import graphics.Shape;
import graphics.ShapeFactory;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;

/**
 * ButtonController is a singleton class which processes mouse click events on
 * buttons. In current version, it only processes the buttons which is used to
 * select drawing tools.
 * 
 * @author Jie Zhao(288654)
 * 
 */
public class ButtonController implements ActionListener {

	private static ButtonController instance = null;
	private DrawingController drawingController = null;

	/**
	 * Return the instance of the ButtonController.
	 * 
	 * @return
	 */
	public static ButtonController getInstance() {
		if (instance == null) {
			instance = new ButtonController();
		}
		return instance;
	}

	private ButtonController() {
		drawingController = DrawingController.getInstance();
	}

	@Override
	/**
	 * The common method processes all mouse click which is used to select
	 * drawing tools.
	 */
	public void actionPerformed(ActionEvent e) {
		AbstractButton button = null;
		if (e.getSource() instanceof AbstractButton) {
			button = (AbstractButton) e.getSource();
			String shapeName = button.getText();
			Shape s = ShapeFactory.createShapeByName(shapeName);
			if (s == null) {
				System.out.println("Shape named " + shapeName
						+ " cannot be created!");
				return;
			}
			drawingController.setCurrentDrawing(s);
		}
	}
}
